

def lausnar(inntak):
	tomurListi = []
	if hreyfing(inntak, (0, 0), tomurListi):
		return "Terminates"
	return "Infinite loop"
		
def gaHvortHringur(skiptirEkki, geyma, iThessu):
	if geyma in iThessu:
		return False
	
	iThessu.append(geyma)
	return hreyfing(skiptirEkki, geyma, iThessu)
	
	
def hreyfing(bord, stada, listi):
	takn = {"^", "v", "<", ">", "@"}
	if bord[stada[0]][stada[1]] == "^":
		k = 1
		while bord[stada[0]+k][stada[1]] not in takn:
			k += 1
		if bord[stada[0]+k][stada[1]] != "@":
			return gaHvortHringur(bord, (stada[0]+k, stada[1]), listi)
		return True
		
	if bord[stada[0]][stada[1]] == "v":
		k = -1
		while bord[stada[0]+k][stada[1]] not in takn:
			k -= 1
		if bord[stada[0]+k+1][stada[1]] != "@":
			return gaHvortHringur(bord, (stada[0]+k, stada[1]), listi)
		return True
		
	if bord[stada[0]][stada[1]] == "<":
		k = -1
		while bord[stada[0]][stada[1] + k] not in takn:
			k -= 1
		if bord[stada[0]][stada[1]+k] != "@":
			return gaHvortHringur(bord, (stada[0], stada[1]+k), listi)
		return True
	
	if bord[stada[0]][stada[1]] == ">":
		k = 1
		while bord[stada[0]][stada[1] + k] not in takn:
			k += 1
		if bord[stada[0]][stada[1]+k] != "@":
			return gaHvortHringur(bord, (stada[0], stada[1]+k), listi)
		return True
	
	if bord[stada[0]][stada[1]] == "@":
		return True
		
oft = int(input())

for x in range(oft):
	k = 0
	mengi = []
	mengi.append(input())
	while mengi[k] != "#":
		mengi.append(input())
		k += 1
	if mengi[-1] == "#":
		mengi.pop(-1)
	print(lausnar(mengi))
